package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Operator;
import cn.wolfcode.crm.service.IOperatorService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/operator")
public class OperatorController {
    @Autowired
    private IOperatorService operatorService;

    //员工页面列表数据
    @RequestMapping("")
    public String index() {
        return "operator";
    }


    @RequestMapping("list")
    @ResponseBody
    public PageResult list(Model model) {
        return operatorService.list();
    }




    @RequestMapping("listAll")
    @ResponseBody
    public List<Operator> listAll(Model model) {
        return operatorService.listAll();
    }


    @RequestMapping("save")
    @ResponseBody

    public JsonResult save(Operator operator) {
        JsonResult jsonResult = new JsonResult();
        try {
            operatorService.save(operator);
            operatorService.insertOperatorRole(operator);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonResult;
    }

    @RequestMapping("update")
    @ResponseBody

    public JsonResult update(Operator operator) {
        JsonResult jsonResult = new JsonResult();
        try {
            operatorService.update(operator);
            operatorService.updateOperatorRole(operator);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonResult;
    }

    //删除员工
    @RequestMapping("delete")
    @ResponseBody

    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            operatorService.delete(id);
            operatorService.deleteOperatorRole(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonResult;
    }

}